'use strict';

/**
 * @ngdoc service
 * @name jgappApp.RongYapi
 * @description
 * # RongYapi
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .factory('Groups', function() {
        var groups = [];

        return {
            isEmpty: function() {
                return !groups.length;
            },

            all: function() {
                return groups;
            },
            push: function(val) {
                groups.push(val);
            },
            remove: function(id) {
                groups = groups.filter(function(item, index) {
                    return item.pkId != id
                });
            },
            set: function(val) {
                groups = val;
            },
            get: function(groupId) {
                // Simple index lookup
                var retIndex = -1;
                for (var i = 0; i < groups.length; i++) {
                    if (groups[i].pkId == groupId) {
                        retIndex = i;
                        break;
                    }
                }
                return retIndex > -1 ? groups[retIndex] : null;
            }
        }
    })
    .factory('ChatUser', function() {
        var lists = [
            // { id: 'group1', username: 'group1', portrait: 'img/personPhoto.png' },
            // { id: 'grp6', username: 'grp6', portrait: 'img/personPhoto.png'}
        ];
        return {
            DiscussionImg: "./images/discussion.png",
            GroupImg: "./images/group.png",
            Private: "./images/user.png",
            all: function() {
                return lists;
            },
            isEmpty: function() {
                return !lists.length;
            },
            set: function(val) {
                lists = val;
            },
            getAllName: function(idArr) {
                var result = [];
                if (typeof idArr == "string") {
                    idArr = [idArr]
                }
                result = this.getAll(idArr).map(function(item, index) {
                    return item.name
                })
                return result.toString();
            },
            getAll: function(idArr) {
                var result = [];
                var _this = this;
                idArr = idArr || [];
                idArr.forEach(function(id, index) {
                    if (_this.get(id)) {
                        result.push(_this.get(id))
                    }
                })
                return result;
            },
            get: function(userId) {
                // Simple index lookup
                var retIndex = -1;
                for (var i = 0; i < lists.length; i++) {
                    if (lists[i].userId == userId) {
                        retIndex = i;
                        break;
                    }
                }
                return retIndex > -1 ? lists[retIndex] : null;
            }
        }
    })
    .factory('ChatStatus', function() {
        return {
            UPDATE: "update",
            CREATE: "create",
            PRIVATE: "PRIVATE",
            DISCUSSION: "DISCUSSION",
            GROUP: "GROUP",
            setName: function(name) {
                return this.groupName = name;
            },
            getName: function() {
                return this.groupName;
            },
            getTargetId: function() {
                return this.targetId;
            },
            setTargetId: function(targetId) {
                return this.targetId = targetId
            },
            setChatType: function(type) {
                return this.type = type;
            },
            getChatType: function() {
                return this.type;
            },
            setStatus: function(status) {
                return this.status = status;
            },
            getStatus: function() {
                return this.status
            },
            setUsers: function(users) {
                return this.users = users;
            },
            getUsers: function() {
                return this.users
            }
        }
    })
    .service("RongYapi", function($http, openApi) {
        var FAKE_Sever = openApi.getToken;
        // var FAKE_Sever = "http://webim.demo.rong.io/";
        //  var headers = {
        //     "Content-Type": "application/x-www-form-urlencoded"
        //  };

        var headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' };
        var RongYapi = {
            getToken: function(data) { //data{email:,password:,env}
                // var req = {
                //          method: "POST",
                //          url: FAKE_Sever + 'email_login_token',
                //          data: 'email='+data.email+'&password='+data.password,
                //          headers: headers
                //      };
                var req = {
                    method: "POST",
                    url: openApi.getToken,
                    headers: headers
                };
                return $http(req);
            },
            getFriend: function() {
                var req = {
                    method: "GET",
                    url: openApi.getgrouplist
                };
                return $http(req);
            },
            getMyGroups: function() {
                var req = {
                    method: "GET",
                    url: openApi.getgrouplist
                };
                return $http(req);
            }
        };
        return RongYapi;
    });
